//package com.itheima.web.old;
///**
// * Create By Awen on 2021/8/22
// *
// * @author Awen
// */
//
//import com.itheima.pojo.Result;
//import com.itheima.pojo.User;
//import com.itheima.service.UserService;
//import com.itheima.utils.AppJwtUtil;
//import com.itheima.utils.CommonsUtils;
//import com.itheima.web.BaseController;
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//@WebServlet("/loginServlet")
//public class LoginServlet extends HttpServlet {
//    @Override
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        User user = null;
//        try {
//            user = CommonsUtils.beanPopulate(User.class, request);
//            UserService userService = new UserService();
//            User u = userService.login(user);
//            if (u == null) {
//                Result result = new Result(false, "用户名或密码错误", null);
//                BaseController.responseJson(response, result);
//            } else {
//                // 登录成功后,生成token并返回给浏览器
//                // 将用户信息保存到redis中,redis的键为id的值
//                String token = AppJwtUtil.getToken(u.getId());
//                System.out.println("登录成功，生成的token值： " + token);
//                BaseController.responseJson(response, new Result(true, "登录成功", token));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            Result result = new Result(false,"服务器忙...",null);
//            BaseController.responseJson(response,result);
//        }
//
//
//
//    }
//
//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        doGet(request, response);
//    }
//}
